package yxy.apple.http.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import yxy.apple.http.HTTPException;
import yxy.apple.util.http.HTTPProtocol;

public class JSONRequest extends PostRequest<JSONObject> {

	public JSONRequest(String url) {
		super(url);
		this.setHeader(HTTPProtocol.HeaderKey.ContentType, HTTPProtocol.MimeType.JSON);
	}

	@Override
	protected String bodyToString(JSONObject body) {
		if (body != null) {
			return JSON.toJSONString(body, true);
		} else {
			return null;
		}
	}

	@Override
	public byte[] encodedBody() throws HTTPException {
		if (this.body() != null) {
			return JSON.toJSONBytes(this.body());
		} else {
			return null;
		}
	}

}
